<?php
namespace app\admin\model;

use app\admin\model\getdatas\MySubject;
use think\facade\Db;

class MyCid
{
    protected $cid = '';
    protected $model='';
    protected $field = [];

    /**设置cid
     * @param null $cid
     * @return $this
     */
    public function setCid($cid=null){
        if($cid){
            $this->cid = $cid;
        }
        return $this;
    }

    /**获取cid
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCid(){
        $data = Db::name(getTable('column'))
            ->where('id',$this->cid)
            ->find();
        return $data;
    }

    /**获取cid字段
     * @param string $cid
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getField($cid=''){
        if($cid){
            $this->setCid($cid);
        }
        $data = Db::name(getTable('ziduancid'))
            ->where('cid',$this->cid)
            ->order('sort desc,id asc')
            ->select()->toArray();
        return $data;
    }

    /**获取模型
     * @param null $cid
     * @param string $field
     * @return array|mixed|\PDOStatement|string|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getModel($cid=null,$field='table'){
        if($cid){
            $this->cid = $cid;
        }
        $cid = $this->getCid();
        $data = Db::name(getTable('models'))
            ->where('id',$cid['model_id'])
            ->find();
        if($field){
            return $data[$field];
        }else{
            return $data;
        }
    }

    /**创建数据
     * @param $model_id
     * @param $cid
     * @return bool
     * @throws \think\Exception
     */
    static public function createTable($model_id,$cid){
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $field = $obj->getField();
        $newvalue = [];
        foreach ($field as $value){
            unset($value['id']);
            unset($value['model_id']);
            $value['cid'] = $cid;
            $newvalue[] = $value;
        }
        if(Db::name(getTable('ziduancid'))->insertAll($newvalue)){
            return true;
        }else{
            return false;
        }
    }

    /**更新cid字段
     * @param $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function updateField($data){
        $model_id = $data['model_id'];
        unset($data['model_id']);
        $cidlist = Db::name(getTable('column'))
            ->where('model_id',$model_id)
            ->select()->toArray();
        foreach ($cidlist as $value){
            $data['cid'] = $value['id'];
            Db::name(getTable('ziduancid'))
                ->insert($data);
        }
        return true;
    }

    /**更新栏目
     * @param $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    static public function updateEdit($data){
        if($data['model_id']??null){
            $obj = new MySubject();
            $obj->setCid($data['id']);
            $model = $obj->getModel();
            if($model){
                if($model['id']!==$data['model_id']){
                    MyCid::deleteCid($data['id']);
                }
            }else{
                MyCid::deleteCid($data['id']);
            }
            return MyCid::createTable($data['model_id'],$data['id']);
        }else{
            return true;
        }
    }

    /**删除数据
     * @param $cid
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    static public function deleteCid($cid){
        return Db::name(getTable('ziduancid'))
            ->where('cid','in',$cid)
            ->delete();
    }

    /**表字段更新时
     * @param $field
     * @param $model_id
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function deleteField($field,$model_id){
        if(!$field){
            return true;
        }
//        return $model_id;
        $cid = Db::name(getTable('column'))
            ->where('model_id',$model_id)
            ->select()->toArray();
        $newcid = [];
        foreach ($cid as $value){
            $newcid[]=$value['id'];
        }
        foreach ($field as $value){
            Db::name(getTable('ziduancid'))
                ->where('cid','in',$newcid)
                ->where('field',$value['field'])
                ->delete();
        }
        return true;
    }

    /**删除表时
     * @param $model_id
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    static public function deleteModel($model_id){
        $cid = Db::name(getTable('column'))
            ->where('model_id',$model_id)
            ->select()->toArray();
        $newcid = [];
        foreach ($cid as $value){
            $newcid[] = $value['id'];
        }
       Db::name(getTable('ziduancid'))
           ->where('cid','in',$newcid)
           ->delete();
        return true;
    }


    public function getData($data){
        $field = $this->getField();
        $newdata = [];
        if($data['id']??null){
            $newdata['id'] = $data['id'];
        }
        foreach ($field as $value){
            if($value['form_type']=='无限联动框'||$value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='编辑框(layui)'||$value['form_type']=='单图片'||$value['form_type']=='视频'||$value['form_type']=='多图片'||$value['form_type']=='单文件'||$value['form_type']=='多文件'||$value['form_type']=='联动下拉框'||$value['form_type']=='联动下拉组'){
                if(isset($data[$value['field']])&&($data[$value['field']]||$data[$value['field']]==0)){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='时间'){
//                echo $value['field'].':'.$data[$value['field']];
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = strtotime($data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = 1;
                }else{
                    $newdata[$value['field']] = 0;
                }
            }
            if($value['form_type']=='单选框'||$value['form_type']=='联动单选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'||$value['form_type']=='联动多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='json数组'){
                $options = explode(':',$value['option']);
                $shujuus = $data[$value['field']];
                $newshuju = [];
                foreach ($shujuus[$options[0]] as $key=>$v){
                    $newshuju[$v] = $shujuus[$options[1]][$key];
                }
                $newdata[$value['field']] = json_encode($newshuju,JSON_UNESCAPED_UNICODE);
            }
        }
        return $newdata;
    }
}